package com.fr.data.impl.sap;

import com.fr.data.core.db.dml.Table;
import com.fr.stable.xml.XMLPrintWriter;
import com.fr.stable.xml.XMLable;
import com.fr.stable.xml.XMLableReader;

import java.util.ArrayList;
import java.util.List;

public class SAPTransfer implements XMLable {
    public static final String XML_TAG = "SAPTRANSFER";

    public static final int SUBMIT_TYPE_INSERT = 0;
    public static final int SUBMIT_TYPE_INTELLI = 1;
    public static final int SUBMIT_TYPE_UPDATE = 2;
    public static final int SUBMIT_TYPE_DELETE = 3;

    private String SAPTableDataName;
    private String exportDataBaseName;
    private Table exportTable;
    private int submitType = SUBMIT_TYPE_INSERT;
    private List columnConfigList = new ArrayList();

    public String getSAPTableDataName() {
        return SAPTableDataName;
    }

    public void setSAPTableDataName(String sAPTableDataName) {
        SAPTableDataName = sAPTableDataName;
    }

    public String getExportDataBaseName() {
        return exportDataBaseName;
    }

    public void setExportDataBaseName(String exportDataBaseName) {
        this.exportDataBaseName = exportDataBaseName;
    }

    public Table getExportTable() {
        return exportTable;
    }

    public void setExportTable(Table exportTable) {
        this.exportTable = exportTable;
    }

    public int getColumnConfigCount() {
        return columnConfigList.size();
    }

    public void addColumnConfig(SAPTransferColumnConfig config) {
        this.columnConfigList.add(config);
    }

    public SAPTransferColumnConfig getColumnConfig(int index) {
        return (SAPTransferColumnConfig) this.columnConfigList.get(index);
    }

    public void removeAllColumnConfig() {
        this.columnConfigList.clear();
    }

    public SAPTransferColumnConfig[] getColumnConfigs() {
        return (SAPTransferColumnConfig[]) this.columnConfigList.toArray(new SAPTransferColumnConfig[0]);
    }

    public int getSubmitType() {
        return submitType;
    }

    public void setSubmitType(int submitType) {
        this.submitType = submitType;
    }

    public void readXML(XMLableReader reader) {
        if (reader.isAttr()) {
            this.removeAllColumnConfig();
            this.SAPTableDataName = reader.getAttrAsString("sapname", null);
            this.exportDataBaseName = reader.getAttrAsString("dbname", null);
            this.submitType = reader.getAttrAsInt("stype", SUBMIT_TYPE_INSERT);
        } else if (reader.isChildNode()) {
            String tagName = reader.getTagName();
            if (Table.XML_TAG.equals(tagName)) {
                this.exportTable = new Table();
                reader.readXMLObject(this.exportTable);
            } else if (SAPTransferColumnConfig.XML_TAG.equals(tagName)) {
                SAPTransferColumnConfig cc = new SAPTransferColumnConfig();
                reader.readXMLObject(cc);
                this.addColumnConfig(cc);
            }
        }
    }

    public void writeXML(XMLPrintWriter writer) {
        writer.startTAG(XML_TAG).attr("sapname", this.SAPTableDataName)
                .attr("dbname", this.exportDataBaseName)
                .attr("stype", this.submitType);
        if (this.exportTable != null) {
            this.exportTable.writeXML(writer);
        }
        for (int i = 0; i < this.columnConfigList.size(); i++) {
            ((SAPTransferColumnConfig) this.columnConfigList.get(i)).writeXML(writer);
        }
        writer.end();
    }

    public Object clone() throws CloneNotSupportedException {
        SAPTransfer s = (SAPTransfer) super.clone();
        s.columnConfigList = new ArrayList();
        for (int i = 0; i < this.columnConfigList.size(); i++) {
            s.columnConfigList.add(this.columnConfigList.get(i));
        }
        if (this.exportTable != null) {
            s.exportTable = (Table) this.exportTable.clone();
        }
        return super.clone();
    }
}
